package com.zhoug.zxing3demo;

import android.Manifest;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;

import com.zhoug.permission3.ZPermissions;
import com.zhoug.zxing3.utils.ScanCodeHelper;

public class MainActivity extends MyBaseActivity {
    private ScanCodeHelper mScanCodeHelper;
    private Button mBtn1;
    private Button mBtn2;
    private TextView mTvResult;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_main;
    }

    @Override
    protected void findViews() {

        mBtn1 = findViewById(R.id.btn1);
        mBtn2 = findViewById(R.id.btn2);
        mTvResult = findViewById(R.id.tv_result);
    }

    @Override
    protected void onCreate1(Bundle savedInstanceState) {
//        mScanCodeHelper = new ScanCodeHelper(this,this,getLifecycle());
        mScanCodeHelper = new ScanCodeHelper(this);
        mScanCodeHelper.setChooseImageEnable(true);
        mScanCodeHelper.setScanRectBgEnable(false);
        mScanCodeHelper.setSearchEnable(true);
        mScanCodeHelper.setCallback((code, msg) -> {
            if (code == ScanCodeHelper.Callback.CODE_SUCCESS) {
                mTvResult.setText("成功: " + msg);
            } else {
                mTvResult.setText("失败: " + msg);
            }
        });

        mBtn1.setOnClickListener(v -> {
            ZPermissions.with(this)
                    .setPermissions(Manifest.permission.CAMERA)
                    .setDescription("扫码需要相机权限")
                    .setCallback(((success, granted, denied) -> {
                        if (success) {
                            mScanCodeHelper.start();
                        }
                    }))
                    .request();
        });
        mBtn2.setOnClickListener(v -> {
            ZPermissions.with(this)
                    .setPermissions(Manifest.permission.CAMERA,
                            Manifest.permission.READ_EXTERNAL_STORAGE,
                            Manifest.permission.WRITE_EXTERNAL_STORAGE)
                    .setDescription("扫码需要相机权限")
                    .setCallback(((success, granted, denied) -> {
                        if (success) {
                            mScanCodeHelper.start();
                        }
                    }))
                    .request();

        });


    }




}